/**
 * Copyright (c) 2020 wuchenxi
 * walter-blog is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * <p>
 * <p>
 * Mulan Permissive Software License，Version 2
 * <p>
 * Mulan Permissive Software License，Version 2 (Mulan PSL v2)
 * January 2020 http://license.coscl.org.cn/MulanPSL2
 */
package com.walter.auth.config.user;

import cn.hutool.core.collection.CollectionUtil;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户端用户信息
 *
 * @author wuchenxi
 * @date 2021-06-04 07:38:26
 */
@Slf4j
@ToString
public class BlogUser implements UserDetails {
    private static final long serialVersionUID = 530L;
    private final String password;
    /**
     * 用户id
     */
    private final String Id;
    private final String account;
    private final String nickName;
    private final Set<GrantedAuthority> authorities;
    private final boolean accountNonExpired;
    private final boolean accountNonLocked;
    private final boolean credentialsNonExpired;
    private final boolean enabled;

    public BlogUser() {
        this(null, null, null, null, null, false, false, false, false);
    }

    public BlogUser(String account, String userId, String nickName, String password, Collection<? extends GrantedAuthority> authorities, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked) {
        this.account = account;
        this.Id = userId;
        this.nickName = nickName;
        this.enabled = enabled;
        this.password = password;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        if (CollectionUtil.isEmpty(authorities)) {
            this.authorities = new HashSet<>();
        } else {
            this.authorities = new HashSet<>(authorities);
        }
    }

    public BlogUser(String account, String userId, String nickName, String password, Collection<? extends GrantedAuthority> authorities) {
        this(account, userId, nickName, password, authorities, true, true, true, true);
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    public String getId() {
        return Id;
    }

    public String getAccount() {
        return account;
    }

    public String getNickName() {
        return nickName;
    }
}
